//
//  HMStatusRetweetView.swift
//  Black22
//
//  Created by Apple on 16/11/22.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import YYText

//  转发微博视图
class HMStatusRetweetView: UIView {
    //  记录转发微博视图的底部约束
    var retweetViewBottomConstraint: Constraint?
    
    var statusViewModel: HMStatusViewModel? {
        didSet {
//            contentLabel.text = statusViewModel?.retweetContent
            contentLabel.attributedText = statusViewModel?.retweetAttributedStr
            retweetViewBottomConstraint?.uninstall()
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  显示配图
                pictureView.isHidden = false
                //  有转发配图
                pictureView.picUrls = picUrls
                self.snp_updateConstraints(closure: { (make) in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
                })
                
                
            } else {
                pictureView.isHidden = true
                //  更新约束
                self.snp_updateConstraints(closure: { (make) in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(10).constraint
                })
            }
        }
    }

    // MARK:    --懒加载   
    fileprivate lazy var contentLabel: YYLabel = {
//        let label = UILabel(fontSize: 14, textColor: UIColor.lightGray)
        let label = YYLabel()
        //  最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 20
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    //  配图视图
    fileprivate lazy var pictureView: HMStatusPictureView = {
        let view = HMStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //  设置背景色
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        addSubview(contentLabel)
        addSubview(pictureView)
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(10)
            make.top.equalTo(self).offset(10)
            make.width.equalTo(self).offset(-20)
            
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            
        }
        
        //  关键约束
        self.snp_makeConstraints { (make) in
            self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        
        //  点击高亮执行的闭包
        contentLabel.highlightTapAction = { (containerView: UIView, attributedText: NSAttributedString, range: NSRange, rect: CGRect) -> Void in
            let result = (attributedText.string as NSString).substring(with: range)
            print(result)
        }
    }
    
    

}
